﻿<Local:NavigationView
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    x:Name="PageRoot"
    x:Class="SeriesManager.View.EpisodeView"
    NavigationCacheMode="Enabled"
    
    xmlns:Control="using:SeriesManager.Control"
    xmlns:Converter="using:SeriesManager.Converter"
    xmlns:Local="using:SeriesManager.View">

    <Page.Resources>
        <Converter:EnumStringConverter x:Key="EnumStringConverter" />
        <Converter:EnumBrushConverter x:Key="EnumBrushConverter" />
        <Converter:BooleanVisibilityConverter x:Key="BooleanVisibilityConverter" />
        <Converter:EnumBooleanConverter x:Key="EnumBooleanConverter" />
        
        <DataTemplate x:Key="dataTemplate">
            <Grid Margin="30,140,30,30">
                <Grid.RowDefinitions>
                    <RowDefinition Height="auto" />
                    <RowDefinition Height="auto" />
                    <RowDefinition />
                </Grid.RowDefinitions>

                <ItemsControl ItemsSource="{Binding ElementName=PageRoot, Path=DataContext.MetaData[0]}" Margin="0,0,0,20">
                    <ItemsControl.ItemTemplate>
                        <DataTemplate>
                            <Border Background="#A30707" Padding="6" Margin="0,0,10,5">
                                <TextBlock Foreground="White" Text="{Binding}" FontWeight="Bold" FontSize="20" />
                            </Border>
                        </DataTemplate>
                    </ItemsControl.ItemTemplate>
                    <ItemsControl.ItemsPanel>
                        <ItemsPanelTemplate>
                            <Control:WrapPanel BlockSize="40" Orientation="Horizontal" Margin="0,0,0,10" />
                        </ItemsPanelTemplate>
                    </ItemsControl.ItemsPanel>
                </ItemsControl>

                <StackPanel Grid.Row="1" HorizontalAlignment="Center">
                    <Border Background="Black">
                        <Image Source="{Binding Image.LocalPath, TargetNullValue=/Assets/EpisodeViewPlaceholder.png}" Width="400" Stretch="UniformToFill" />
                    </Border>

                    <Button Background="{Binding State, Converter={StaticResource EnumBrushConverter}}" Content="{Binding State, Converter={StaticResource EnumStringConverter}}" Command="{Binding ElementName=PageRoot, Path=DataContext.StateCommand}" Style="{StaticResource EpisodeStateButtonStyle}" />
                </StackPanel>

                <ScrollViewer Grid.Row="2" VerticalAlignment="Top" Padding="16,14,16,8" HorizontalScrollMode="Disabled" HorizontalScrollBarVisibility="Disabled" VerticalScrollMode="Auto" VerticalScrollBarVisibility="Auto" Margin="0,20,0,0" Visibility="{Binding ElementName=PageRoot, Path=DataContext.OverviewVisibility, Converter={StaticResource BooleanVisibilityConverter}}">
                    <ScrollViewer.Background>
                        <SolidColorBrush Color="Black" Opacity="0.8" />
                    </ScrollViewer.Background>

                    <TextBlock Text="{Binding Overview}" FontSize="16" TextWrapping="Wrap" LineHeight="26" Foreground="White" />
                </ScrollViewer>
            </Grid>
        </DataTemplate>
    </Page.Resources>

    <Grid>
        <Grid.ChildrenTransitions>
            <TransitionCollection>
                <EntranceThemeTransition/>
            </TransitionCollection>
        </Grid.ChildrenTransitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="140"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        
        <FlipView
            x:Name="flipView"
            Grid.RowSpan="2"
            ItemsSource="{Binding View}">
            <FlipView.ItemTemplate>
                <DataTemplate>
                    <Grid Margin="116,140,60,40">
                        <Grid.RowDefinitions>
                            <RowDefinition Height="auto" />
                            <RowDefinition />
                        </Grid.RowDefinitions>

                        <ItemsControl ItemsSource="{Binding ElementName=PageRoot, Path=DataContext.MetaData}" Margin="0,0,0,20">
                            <ItemsControl.ItemTemplate>
                                <DataTemplate>
                                    <ItemsControl ItemsSource="{Binding}">
                                        <ItemsControl.ItemTemplate>
                                            <DataTemplate>
                                                <Border Background="#A30707" Padding="6" Margin="0,0,10,5">
                                                    <TextBlock Foreground="White" Text="{Binding}" FontWeight="Bold" FontSize="20" />
                                                </Border>
                                            </DataTemplate>
                                        </ItemsControl.ItemTemplate>
                                        <ItemsControl.ItemsPanel>
                                            <ItemsPanelTemplate>
                                                <Control:WrapPanel BlockSize="40" Orientation="Horizontal" Margin="0,0,0,10" />
                                            </ItemsPanelTemplate>
                                        </ItemsControl.ItemsPanel>
                                    </ItemsControl>
                                </DataTemplate>
                            </ItemsControl.ItemTemplate>
                            <ItemsControl.ItemsPanel>
                                <ItemsPanelTemplate>
                                    <StackPanel Orientation="Vertical"  />
                                </ItemsPanelTemplate>
                            </ItemsControl.ItemsPanel>
                        </ItemsControl>                       
                        
                        <Grid Grid.Row="1">
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="400" />
                                <ColumnDefinition />
                            </Grid.ColumnDefinitions>

                            <StackPanel Orientation="Vertical">
                                <Border Background="Black">
                                    <Image Source="{Binding Image.LocalPath, TargetNullValue=/Assets/EpisodeViewPlaceholder.png}" Stretch="UniformToFill" />
                                </Border>
                                <Button Background="{Binding State, Converter={StaticResource EnumBrushConverter}}" Content="{Binding State, Converter={StaticResource EnumStringConverter}}" Command="{Binding ElementName=PageRoot, Path=DataContext.StateCommand}" Style="{StaticResource EpisodeStateButtonStyle}" />
                            </StackPanel>

                            <ScrollViewer Margin="60,0,0,0" Grid.Column="1" VerticalAlignment="Top" Padding="16,14,16,8" HorizontalScrollMode="Disabled" HorizontalScrollBarVisibility="Disabled" VerticalScrollMode="Auto" VerticalScrollBarVisibility="Auto" Visibility="{Binding ElementName=PageRoot, Path=DataContext.OverviewVisibility, Converter={StaticResource BooleanVisibilityConverter}}">
                                <ScrollViewer.Background>
                                    <SolidColorBrush Color="Black" Opacity="0.8" />
                                </ScrollViewer.Background>
                                    
                                <TextBlock Text="{Binding Overview}" FontSize="16" TextWrapping="Wrap" LineHeight="26" Foreground="White" />
                            </ScrollViewer>
                        </Grid>
                    </Grid>
                </DataTemplate>
            </FlipView.ItemTemplate>
        </FlipView>
        
        <CommandBar IsOpen="{Binding NavigationBarIsOpen, Mode=TwoWay}" Grid.Row="1">
            <AppBarToggleButton Icon="Accept" x:Uid="EpisodeStateAppBarToggleButton" IsChecked="{Binding View.CurrentItem.State, Converter={StaticResource EnumBooleanConverter}}" Command="{Binding StateCommand}" />
        </CommandBar>

        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="120"/>
                <ColumnDefinition Width="*"/>
            </Grid.ColumnDefinitions>
            <AppBarButton Icon="Back" Height="95" Margin="10,46,10,0" Command="{Binding BackCommand}" Visibility="{Binding IsEnabled, Converter={StaticResource BooleanVisibilityConverter}, RelativeSource={RelativeSource Mode=Self}}"/>
            <TextBlock TextWrapping="NoWrap" Text="{Binding Title}" Style="{StaticResource HeaderTextStyle}" Grid.Column="1" IsHitTestVisible="False" VerticalAlignment="Bottom" Margin="0,0,20,40" TextTrimming="CharacterEllipsis"/>
        </Grid>

        <VisualStateManager.VisualStateGroups>
            <VisualStateGroup>
                <VisualState x:Name="Landscape500">
                    <Storyboard>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="flipView" Storyboard.TargetProperty="ItemTemplate">
                            <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource dataTemplate}"/>
                        </ObjectAnimationUsingKeyFrames>
                    </Storyboard>
                </VisualState>
                <VisualState x:Name="Landscape768" />
            </VisualStateGroup>
        </VisualStateManager.VisualStateGroups>
    </Grid>
    
</Local:NavigationView>